<template>
  <div class="app-container">
    <el-table v-loading="loading" :data="tableData">
      <el-table-column label="审核项" align="center" prop="reviewName">
      </el-table-column>
      <el-table-column label="说明" align="center" prop="direction">
      </el-table-column>
      <el-table-column label="最后操作时间" align="center" prop="updateTime">
      </el-table-column>
      <el-table-column label="是否需要审核" align="center">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            @change="updateHandle(scope.row)"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="0"
            :inactive-value="1"
          >
          </el-switch>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import {
  listPost_reviewConfig,
  updatePost_reviewConfig,
  preCheck,
} from "@/api/configuration/auditConfiguration";
export default {
  name: "auditConfiguration",
  data() {
    return {
      // 表格数据
      tableData: [],
      info: {},
      loading: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 获取配置信息 */
    async getList() {
      this.loading = true;
      let res = await listPost_reviewConfig({ reviewItem: "" });
      this.loading = false;
      this.tableData = res.rows;
    },
    async updateHandle(item) {
      let pre = await preCheck(item.id);
      let txt;
      let type = item.reviewItem === 1 ? "知识" : "课程";
      let regex = new RegExp(type + "(?!.*" + type + ")");
      //  知识点和课程
      if (item.reviewItem == 1 || item.reviewItem == 0) {
        // 关闭确认
        if (item.status == 1) {
          if (pre.data.num == 0) {
            txt = `<div>确定关闭【${
              item.reviewName
            }】吗?关闭后，提交审核的${type}将跳过${item.reviewName.replace(
              regex,
              ""
            )}步骤，请谨慎操作</div>`;
          } else {
            txt = `<div>1、当前有${
              pre.data.num
            }个${type}待【${item.reviewName.replace(regex, "")}】，
            关闭【${item.reviewName}】后，会自动通过当前审核<div>
            <div>2.关闭后,提交审核的${type}将跳过${item.reviewName.replace(
              regex,
              ""
            )}步骤</div>`;
          }
        }
        // 开启确认
        else {
          txt = `<div>确定开启【${
            item.reviewName
          }】吗?开启后，提交审核的${type}需要${item.reviewName.replace(
            regex,
            ""
          )}通过后方可使用</div>`;
        }
      }
      // 店铺删除
      else {
        // 关闭确认
        if (item.status == 1) {
          if (pre.data.num > 0) {
            txt = `<div>1、当前有${pre.data.num}个门店删除待审核，
            关闭【${item.reviewName}】后，会自动通过当前审核<div>
            <div>2.关闭后,人资、品控专员、培训专员可直接删除门店！</div>`;
          } else {
            txt = `<div>确定关闭【${item.reviewName}】吗?关闭后，人资、品控专员、培训专员可直接删除门店</div>`;
          }
        }
        // 开启确认
        else {
          txt = `<div>确定开启【${item.reviewName}】吗?开启后，人资、品控专员、培训专员删除门店需品控经理审核</div>`;
        }
      }
      this.$confirm(txt, item.status == 1 ? "关闭确认" : "开启确认", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        dangerouslyUseHTMLString: true,
      })
        .then(async () => {
          try {
            let res = await updatePost_reviewConfig(item);
            const { code } = res;
            if (code == 200) {
              this.$modal.msgSuccess("修改成功");
              this.getList();
            } else {
              item.status == 0 ? (item.status = 1) : (item.status = 0);
            }
          } catch (error) {
            item.status == 0 ? (item.status = 1) : (item.status = 0);
          }
        })
        .catch(() => {
          item.status == 0 ? (item.status = 1) : (item.status = 0);
        });
    },
  },
};
</script>

<style scoped lang="scss">
.container {
}
</style>
